package com.isesol.ime.rest.controller;

import com.alibaba.fastjson.*;
import com.google.common.collect.*;
import com.isesol.ime.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;

@RestController
public class EventController{

	@Resource
	EventService eventService;

	private void getDownload(HttpServletResponse response, byte[] bytes,String filename){

		ServletOutputStream ouputStream;
		try{
			ouputStream = response.getOutputStream();
			ZipOutputStream out = new ZipOutputStream(ouputStream);
			out.putNextEntry(new ZipEntry(filename));
			out.write(bytes);
			response.setContentType("application/zip");
			response.addHeader("Content-Disposition", "attachment; filename=data.zip");
			out.finish();
			out.close();
		} catch (IOException e){

		}
	}

	@ResponseBody
	@RequestMapping("/event/{messageId}")
	public void event(@PathVariable String messageId,HttpServletResponse response){

		Map eventMap = Maps.newHashMap();
		eventMap.put("eventData", eventService.getEvents(messageId));

		getDownload(response, JSON.toJSONString(eventMap).getBytes(),"eventData.json");

	}

	@ResponseBody
	@RequestMapping("/message/{messageId}")
	public void message(@PathVariable String messageId,HttpServletResponse response){

		Map eventMap = Maps.newHashMap();
		eventMap.put("eventData", eventService.getSubData(messageId));

		getDownload(response, JSON.toJSONString(eventMap).getBytes(),"subscrData.json");
	}

}
